package com.bandaotixi.cashier.api.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.bandaotixi.cashier.api.business.AuthService;
import com.bandaotixi.cashier.api.business.bo.CaptchaInfo;
import com.bandaotixi.cashier.api.common.result.Result;
import com.bandaotixi.cashier.api.core.aspect.log.Log;
import com.bandaotixi.cashier.api.core.aspect.log.LogModuleEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制层
 *
 */
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@Slf4j
public class AuthController {

    private final AuthService authService;

    @GetMapping("/captcha")
    @SaIgnore
    public Result<CaptchaInfo> getCaptcha() {
        CaptchaInfo captcha = authService.getCaptcha();
        return Result.success(captcha);
    }

    @PostMapping("/login")
    @SaIgnore
    @Log(value = "登录", module = LogModuleEnum.LOGIN)
    public Result<String> login(@RequestParam String username, @RequestParam String password) {
        String authenticationToken = authService.login(username, password);
        return Result.success(authenticationToken);
    }

    @DeleteMapping("/logout")
    public Result<?> logout() {
        try {
            authService.logout();
        } catch (Exception ignored) {
        }
        return Result.success();
    }
}
